﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProBugTracker.Data.Interface;

namespace ProBugTracker.Data
{
    public class UserManagerData : IUserManagerData
    {
        public UserDB AddNewUser(UserDB user)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    
                    proBugTrackerEntities.AddToUserDBs(user);
                    proBugTrackerEntities.SaveChanges();
                    return user;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool EditNewUser(UserDB user)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    UserDB userInfo = (from u in proBugTrackerEntities.UserDBs where u.ID == user.ID select u).FirstOrDefault();
                    userInfo.Password = user.Password;
                    userInfo.FirstName = user.FirstName;
                    userInfo.MiddleName = user.MiddleName;
                    userInfo.LastName = user.LastName;
                    userInfo.Email = user.Email;
                    userInfo.LastChangeDate = DateTime.Now;
                    proBugTrackerEntities.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool DeleteUser(int userID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    UserDB userInfo = (from u in proBugTrackerEntities.UserDBs where u.ID == userID select u).FirstOrDefault();
                    if (userInfo != null)
                    {
                        userInfo.Active = false;
                        proBugTrackerEntities.SaveChanges();
                        return true;
                    }
                    else
                        return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public UserAssignRoleDB AddNewUserRole(UserAssignRoleDB userAssignRole)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    proBugTrackerEntities.AddToUserAssignRoleDBs(userAssignRole);
                    proBugTrackerEntities.SaveChanges();
                    return userAssignRole;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool RemoveUserRole(int userID, int roleID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    UserAssignRoleDB userAssignRole = (from u in proBugTrackerEntities.UserAssignRoleDBs where u.UserID == userID && u.UserRoleID == roleID select u).FirstOrDefault();
                    if (userAssignRole != null)
                    {
                        proBugTrackerEntities.DeleteObject(userAssignRole);
                        proBugTrackerEntities.SaveChanges();
                        return true;
                    }
                    else
                        return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public IList<UserDB> FindAllActiveUsers()
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    IList<UserDB> userList = (from u in proBugTrackerEntities.UserDBs where u.Active == true select u).ToList();
                    return userList;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<UserDB> FindAllInactiveUsers()
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    IList<UserDB> userList = (from u in proBugTrackerEntities.UserDBs where u.Active == false select u).ToList();
                    return userList;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public UserDB FindUserByID(int userID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    UserDB user = (from u in proBugTrackerEntities.UserDBs where u.ID == userID select u).SingleOrDefault();
                    return user;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public UserDB FindUserByUserName(string userName)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    UserDB user = (from u in proBugTrackerEntities.UserDBs where u.UserName == userName select u).SingleOrDefault();
                    return user;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<UserDB> FindUserByFirstName(string firstName)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    IList<UserDB> userList = (from u in proBugTrackerEntities.UserDBs where u.FirstName == firstName select u).ToList();
                    return userList;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
